﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

using MSAcademic.v2.Infrastructure.Login;
using MSAcademic.v2.Infrastructure.PasswordRecovery;
using MSAcademic.v2.Infrastructure.User;

namespace MSAcademic.v2.Web.Areas.Admin.Models.Login
{
    public partial class ResetPasswordModel : IDataErrorInfo
    {
        public string TokenValue { get; set; }
        public bool IsFormSuccess { get; set; }

        [DataType(DataType.Password)]
        [Required(ErrorMessage = "Polje ne smije biti prazno!")]
        [StringLength(40, MinimumLength = 6, ErrorMessage = "Lozinka mora biti između 6 i 40 znakova duljine!")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Required(ErrorMessage = "Polje ne smije biti prazno!")]
        [StringLength(40, MinimumLength = 6, ErrorMessage = "Lozinka mora biti između 6 i 40 znakova duljine!")]
        public string RepeatedNewPassword { get; set; }
    }

    public partial class ResetPasswordModel
    {
        public bool IsTokenVerified
        {
            get
            {
                if (_isTokenVerified == null)
                {
                    _isTokenVerified = new Lazy<bool>(() => _passwordRecoveryService.VerifyToken(this.TokenValue));
                }
                return _isTokenVerified.Value;
            }
        }
        private Lazy<bool> _isTokenVerified;

        public bool IsUserSignedIn
        {
            get
            {
                if (_isUserSignedIn == null)
                {
                    _isUserSignedIn = new Lazy<bool>(() => _loginService.TryGetSignedInUser() != null);
                }
                return _isUserSignedIn.Value;
            }
        }
        private Lazy<bool> _isUserSignedIn;

        public int UserId
        {
            get { return _passwordRecoveryService.TryGetUserIdWhereToken(this.TokenValue); }
        }

        public IUser User
        {
            get
            {
                if (_user == null)
                {
                    _user = new Lazy<IUser>(() => _userQueries.TryGetWhereId(this.UserId));
                }
                return _user.Value;
            }
        }
        private Lazy<IUser> _user;
    }

    public partial class ResetPasswordModel
    {
        public string this[string prop]
        {
            get
            {
                if (prop == "NewPassword")
                {
                    if (this.NewPassword != this.RepeatedNewPassword)
                    {
                        return "Unesene lozinke nisu iste!";
                    }
                }

                return null;
            }
        }

        public string Error
        {
            get { return null; }
        }
    }

    public partial class ResetPasswordModel
    {
        private readonly IPasswordRecoveryService _passwordRecoveryService;
        private readonly ILoginService _loginService;
        private readonly IUserQueries _userQueries;

        public ResetPasswordModel(
            IPasswordRecoveryService passwordRecoveryService,
            ILoginService loginService,
            IUserQueries userQueries)
        {
            _passwordRecoveryService = passwordRecoveryService;
            _loginService = loginService;
            _userQueries = userQueries;
        }
    }
}